//
//  RoyRippleAnimationView.m
//  LEVE
//
//  Created by 王若聪 on 2017/3/11.
//  Copyright © 2017年 dashuju. All rights reserved.
//

#import "RoyRippleAnimationView.h"

@interface RoyRippleAnimationView ()

//@property (nonatomic, strong) UIButton *rippleButton;

@property (nonatomic, strong) UIView *targetView;
@property (nonatomic, strong) NSTimer *rippleTimer;
@property (nonatomic, assign) NSInteger mode;

@end

@implementation RoyRippleAnimationView


- (void)rippleForTargetView:(UIView *)targetView{
    
    _targetView = targetView;
    [self addRippleLayer];
    
    self.rippleTimer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(addRippleLayer) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_rippleTimer forMode:NSRunLoopCommonModes];
    
    
    [targetView addSubview:self];
    
}




- (void)addRippleLayer{
    CAShapeLayer *rippleLayer = [[CAShapeLayer alloc] init];
    rippleLayer.position = CGPointMake(200, 200);
    rippleLayer.bounds = CGRectMake(0, 0, 400, 400);
    rippleLayer.backgroundColor = [UIColor clearColor].CGColor;
    //line path
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(_targetView.bounds.origin.x, _targetView.bounds.origin.y, _targetView.width, _targetView.width)];
    rippleLayer.path = path.CGPath;
    rippleLayer.strokeColor = [UIColor yellowColor].CGColor;
    rippleLayer.lineWidth = 1.5;
    rippleLayer.fillColor = [UIColor clearColor].CGColor;
    [self.layer insertSublayer:rippleLayer below:_targetView.layer];
    //addRippleAnimation
    UIBezierPath *beginPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(_targetView.bounds.origin.x, _targetView.bounds.origin.y, _targetView.width, _targetView.width)];
    CGRect endRect = CGRectInset([self makeEndRect], -100, -100);
    UIBezierPath *endPath = [UIBezierPath bezierPathWithOvalInRect:endRect];
    rippleLayer.path = endPath.CGPath;
    rippleLayer.opacity = 0.0;
    
    CABasicAnimation *rippleAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    rippleAnimation.fromValue = (__bridge id _Nullable)(beginPath.CGPath);
    rippleAnimation.toValue = (__bridge id _Nullable)(endPath.CGPath);
    rippleAnimation.duration = 3.0;
    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnimation.fromValue = [NSNumber numberWithFloat:0.6];
    opacityAnimation.toValue = [NSNumber numberWithFloat:0.0];
    opacityAnimation.duration = 3.0;
    [rippleLayer addAnimation:opacityAnimation forKey:@""];
    [rippleLayer addAnimation:rippleAnimation forKey:@""];
    
    [self performSelector:@selector(removeRippleLayer:) withObject:rippleLayer afterDelay:2.0];
}



- (CGRect)makeEndRect{
    CGRect endRect = CGRectMake(_targetView.bounds.origin.x, _targetView.bounds.origin.y, _targetView.width, _targetView.width);
    endRect = CGRectInset(endRect, -100, -100);
    return endRect;
}

- (void)removeRippleLayer:(CAShapeLayer *)rippleLayer{
    [rippleLayer removeFromSuperlayer];
    rippleLayer = nil;
}

- (void)closeRippleTimer{
    if (_rippleTimer) {
        if ([_rippleTimer isValid]) {
            [_rippleTimer invalidate];
        }
        _rippleTimer = nil;
    }
}

- (void)removeFromParentView{
    if (self.superview) {
        [self closeRippleTimer];
        [self removeAllSubLayers];
        [self removeFromSuperview];
        [self.layer removeAllAnimations];
    }
}

- (void)removeAllSubLayers{
    for (NSInteger i = 0; [self.layer sublayers].count > 0; i++) {
        [[[self.layer sublayers] firstObject] removeFromSuperlayer];
    }
}

@end
